package org.lansir.af.util;

import org.lansir.af.gtalk.GtalkApplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

//偏好设定
public class Prefs {
	// private fields
	private SharedPreferences prefs = null;
	public static final String AutoReceive = "bAutoReceive";

	public int iSnoozeCount; // count of snooze times to change alarm to cleared
	public int iSnoozeMinutesOverdue; // snooze minutes alarm overdue


	public Prefs(Context ctx) {
		prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
		load();
	}

	public boolean saveAll() {
		try {
			SharedPreferences.Editor ed = prefs.edit();

			ed.putInt(GtalkApplication.SNOOZE_COUNT_KEY, iSnoozeCount);
			ed.putInt(GtalkApplication.SNOOZE_MINUTES_OVERDUE, iSnoozeMinutesOverdue);

			
			// return ed.commit();

			ed.commit();

			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean saveInt(String key, int value){
		try {
			SharedPreferences.Editor ed = prefs.edit();
			ed.putInt(key, value);
			ed.commit();
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean saveString(String key, String value){
		try {
			SharedPreferences.Editor ed = prefs.edit();
			ed.putString(key, value);
			ed.commit();
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean saveBoolean(String key, Boolean value){
		try {
			SharedPreferences.Editor ed = prefs.edit();
			ed.putBoolean(key, value);
			ed.commit();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void load() {

		iSnoozeCount = Integer.parseInt(prefs.getString(GtalkApplication.SNOOZE_COUNT_KEY, "5"));
		iSnoozeMinutesOverdue = Integer.parseInt(prefs.getString(GtalkApplication.SNOOZE_MINUTES_OVERDUE, "5"));
	}
	
	public SharedPreferences getPrefs(){
		return this.prefs;
	}
}
